package com.deng.hadoop.mapreduce.demo.weather;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Created By DengLibin
 * Date: 19-4-3
 * Time: 上午10:56
 * Desc: reduce程序
 */
public class WeatherReducer extends Reducer<TianQi, IntWritable, Text, IntWritable> {

    private final  Text rKey = new Text();
    private final IntWritable rValue = new IntWritable();
    /**
     * 年月相同为一组，调一次该方法
     * @param key
     * @param values 温度 已经倒序排好了
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(TianQi key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        boolean b = true;
        Integer day = 0;
        for(IntWritable v : values){
            if(b){ //第一天
                String keyString = key.getYear()+"-"+key.getMonth()+"-"+key.getDay()+":"+key.getTemperature();
                rKey.set(keyString);
                rValue.set(v.get());
                b = false;
                day = key.getDay();
                context.write(rKey, rValue);
                continue;
            }
            if(key.getDay() != day){ //第二天
                String keyString = key.getYear()+"-"+key.getMonth()+"-"+key.getDay()+":"+key.getTemperature();
                rKey.set(keyString);
                rValue.set(v.get());
                context.write(rKey, rValue);
                break;
            }

        }
    }
}




















